package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserAuthMapper;
import com.heima.user.service.ApUserAuthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@Slf4j
public class ApUserAuthServiceImpl extends ServiceImpl<ApUserAuthMapper, ApUserRealname> implements ApUserAuthService {


    /**
     * 查询认证用户列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult queryAuthList(AuthDto dto) {

        //1.检查参数
        if(dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查分页
        dto.checkParam();

        IPage page = new Page(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<ApUserRealname> lambdaQueryWrapper = new LambdaQueryWrapper<>();


        if (dto.getStatus() != null){
            lambdaQueryWrapper.eq(ApUserRealname::getStatus, dto.getStatus());
        }


        page = page(page, lambdaQueryWrapper);

        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 审核失败（驳回）
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authFail(AuthDto dto) {

        //1.检查参数
        if(dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        update(Wrappers.<ApUserRealname>lambdaUpdate()
                .eq(ApUserRealname::getId, dto.getId())
                .set(ApUserRealname::getStatus, 2 )
                .set(ApUserRealname::getReason, dto.getMsg()));

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 审核成功， （修改审核状态）
     * @param id
     */
    @Override
    public void authSuccess(Integer id) {

        update(Wrappers.<ApUserRealname>lambdaUpdate()
                .eq(ApUserRealname::getId, id)
                .set(ApUserRealname::getStatus, 9));

    }


}
